package com.apexsoft.test.sample.ams.producer;

import com.apex.ams.server.AmsService;
import com.apexsoft.test.sample.helloworld.HelloRequest;
import com.apexsoft.test.sample.helloworld.HelloResponse;
import com.apexsoft.test.sample.helloworld.HelloworldServiceGrpc;
import com.apexsoft.test.sample.helloworld.Record;
import io.grpc.stub.StreamObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: initializr
 * @Description:
 * @Date: Created in 2019-10-23 13:43
 * @MODIFIED BY:
 */
@AmsService(version = "1.0.1")
public class ProtobufProducerDemo extends HelloworldServiceGrpc.HelloworldServiceImplBase {

    private static final Logger log = LoggerFactory.getLogger(ProtobufProducerDemo.class);

    @Override
    public void say(HelloRequest req, StreamObserver<HelloResponse> responseObserver){


        HelloResponse.Builder builder = HelloResponse.newBuilder();
        try {
            builder.setCode(1).setNote("[esb]success");
            int count = 10;
            for(int i=1;i<=count;i++){
                Record record = Record.newBuilder()
                        .setId(i)
                        .setRowCode("10001")
                        .setRowName("num10001")
                        .build();
                builder.addRecords(record);
            }
            builder.setCount(count);
        }catch(Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            builder.setCode(-1).setNote("[hello]error:"+e.getMessage());
        }finally {
            HelloResponse resp = builder.build();
            responseObserver.onNext(resp);
            responseObserver.onCompleted();
        }
    }

}
